package com.cn.newetv.utils;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SysCfgDao {
	private MySQLiteOpenHelper mySQLiteOpenHelper = null;
	private SQLiteDatabase sqLiteDatabase = null;
	
	public void openDB(Context context) {
		try {
			mySQLiteOpenHelper = new MySQLiteOpenHelper(context);
			sqLiteDatabase = mySQLiteOpenHelper.getWritableDatabase();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void insert(String key, String value) {
		try {
			String sql = "insert into "+MySQLiteOpenHelper.TABLE_NAME+" (paramkey,paramvalue) values('"+key+"','"+value+"');";
			sqLiteDatabase.execSQL(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void update(String key, String value) {
		try {
			String sql = "update "+MySQLiteOpenHelper.TABLE_NAME+" set paramvalue=? where paramkey=?;";
			sqLiteDatabase.execSQL(sql, new Object[]{value,key});
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void deleteByKey(String key) {
		try {
			String sql = "delete from "+MySQLiteOpenHelper.TABLE_NAME+" where paramkey=?;";
			sqLiteDatabase.execSQL(sql, new Object[]{key});
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void deleteAll() {
		try {
			String sql = "delete from "+MySQLiteOpenHelper.TABLE_NAME+";";
			sqLiteDatabase.execSQL(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	private Cursor selectByKey(String key) {
		Cursor cursor = null;
	  	try {
			String sql = "select paramkey,paramvalue from "+MySQLiteOpenHelper.TABLE_NAME+" where paramkey=?;";
			cursor = sqLiteDatabase.rawQuery(sql, new String[]{key});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cursor;
	}
	
	private Cursor select() {
		Cursor cursor = null;
	  	try {
			String sql = "select paramkey,paramvalue from "+MySQLiteOpenHelper.TABLE_NAME+";";
			cursor = sqLiteDatabase.rawQuery(sql, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cursor;
	}
	
	public Map<String, String> findAll() {
		Map<String, String> res = new HashMap<String, String>();
		try {
			Cursor cursor = select();
			if(cursor == null || cursor.getCount() == 0)
				return res;
			while (cursor.moveToNext()) {
				res.put(cursor.getString(0), cursor.getString(1));
				Log.d("SysCfgDao", cursor.getString(0)+"<===>"+cursor.getString(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public void add(String key, String value) {
		try {
			insert(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void modify(String key, String value) {
		try {
			update(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getByKey(String key) {
		String value = null;
		try {
			Cursor cursor = selectByKey(key);
			if(cursor == null || cursor.getCount() == 0)
				return value;
			cursor.moveToNext();
			value = cursor.getString(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}
	
	public void removeByKey(String key) {
		try {
			deleteByKey(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void removeAll() {
		try {
			deleteAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
